View Javadoc

1   // DefaultHeapAllocator.java, created Mon Apr  9  1:01:31 2001 by joewhaley
2   // Copyright (C) 2001-3 John Whaley <jwhaley@alum.mit.edu>
3   // Licensed under the terms of the GNU LGPL; see COPYING for details.
4   package joeq.Allocator;
5   
6   import joeq.Class.PrimordialClassLoader;
7   import joeq.Class.jq_Class;
8   import joeq.Class.jq_StaticMethod;
9   import joeq.Memory.Address;
10  import joeq.Runtime.Unsafe;
11  import joeq.Scheduler.jq_NativeThread;
12  
13  /***
14   * Provides access functions to the default heap allocator for the current thread.
15   * 
16   * @author  John Whaley <jwhaley@alum.mit.edu>
17   * @version $Id: DefaultHeapAllocator.java 1867 2004-08-07 10:00:39Z joewhaley $
18   */
19  public abstract class DefaultHeapAllocator {
20  
21      /***
22       * The default heap allocator for the current thread.
23       * 
24       * @return default heap allocator for the current thread
25       */
26      public static final HeapAllocator def() {
27          return Unsafe.getThreadBlock().getNativeThread().getHeapAllocator();
28      }
29      
30      /***
31       * Initialize the default heap allocator for the current thread.
32       * 
33       * @throws OutOfMemoryError
34       */
35      public static final void init() throws OutOfMemoryError {
36          def().init();
37      }
38      
39      /***
40       * Allocate an object with the default heap allocator for the current thread.
41       * 
42       * @throws OutOfMemoryError
43       */
44      public static final Object allocateObject(int size, Object vtable) throws OutOfMemoryError {
45          Unsafe.getThreadBlock().disableThreadSwitch();
46          Object o = def().allocateObject(size, vtable);
47          Unsafe.getThreadBlock().enableThreadSwitch();
48          return o;
49      }
50      
51      /***
52       * Allocate an aligned object with the default heap allocator for the current thread.
53       * 
54       * @throws OutOfMemoryError
55       */
56      public static final Object allocateObjectAlign8(int size, Object vtable) throws OutOfMemoryError {
57          Unsafe.getThreadBlock().disableThreadSwitch();
58          Object o = def().allocateObjectAlign8(size, vtable);
59          Unsafe.getThreadBlock().enableThreadSwitch();
60          return o;
61      }
62      
63      /***
64       * Allocate an array with the default heap allocator for the current thread.
65       * 
66       * @throws OutOfMemoryError
67       */
68      public static final Object allocateArray(int length, int size, Object vtable)
69      throws OutOfMemoryError, NegativeArraySizeException {
70          Unsafe.getThreadBlock().disableThreadSwitch();
71          Object o = def().allocateArray(length, size, vtable);
72          Unsafe.getThreadBlock().enableThreadSwitch();
73          return o;
74      }
75      
76      /***
77       * Allocate an aligned array with the default heap allocator for the current thread.
78       * 
79       * @throws OutOfMemoryError
80       */
81      public static final Object allocateArrayAlign8(int length, int size, Object vtable)
82      throws OutOfMemoryError, NegativeArraySizeException {
83          Unsafe.getThreadBlock().disableThreadSwitch();
84          Object o = def().allocateArrayAlign8(length, size, vtable);
85          Unsafe.getThreadBlock().enableThreadSwitch();
86          return o;
87      }
88      
89      /***
90       * Return the amount of free memory in the default heap allocator for the current thread.
91       * 
92       * @throws OutOfMemoryError
93       */
94      public static final int freeMemory() { return def().freeMemory(); }
95      
96      /***
97       * Return the total amount of memory us the default heap allocator for the current thread.
98       * 
99       * @throws OutOfMemoryError
100      */
101     public static final int totalMemory() { return def().totalMemory(); }
102 
103     public static final void collect() {
104         Unsafe.getThreadBlock().disableThreadSwitch();
105         def().collect();
106         Unsafe.getThreadBlock().enableThreadSwitch();
107     }
108     
109     public static final boolean isValidHeapAddress(Address a) {
110         if (HeapAllocator.isInDataSegment(a)) return true;
111         if (jq_NativeThread.allNativeThreadsInitialized()) {
112             for (int i = 0; i < jq_NativeThread.native_threads.length; ++i) {
113                 jq_NativeThread nt = jq_NativeThread.native_threads[i];
114                 if (nt.getHeapAllocator().isInHeap(a)) return true;
115             }
116         } else {
117             jq_NativeThread nt = Unsafe.getThreadBlock().getNativeThread();
118             if (nt.getHeapAllocator().isInHeap(a)) return true;
119         }
120         return false;
121     }
122     
123     public static final void processObjectReference(Address a) {
124         def().processObjectReference(a);
125     }
126 
127     public static final void processPossibleObjectReference(Address a) {
128         def().processPossibleObjectReference(a);
129     }
130     
131     public static final jq_StaticMethod _allocateObject;
132     public static final jq_StaticMethod _allocateObjectAlign8;
133     public static final jq_StaticMethod _allocateArray;
134     public static final jq_StaticMethod _allocateArrayAlign8;
135     static {
136         jq_Class k = (jq_Class)PrimordialClassLoader.loader.getOrCreateBSType("Ljoeq/Allocator/DefaultHeapAllocator;");
137         _allocateObject = k.getOrCreateStaticMethod("allocateObject", "(ILjava/lang/Object;)Ljava/lang/Object;");
138         _allocateObjectAlign8 = k.getOrCreateStaticMethod("allocateObjectAlign8", "(ILjava/lang/Object;)Ljava/lang/Object;");
139         _allocateArray = k.getOrCreateStaticMethod("allocateArray", "(IILjava/lang/Object;)Ljava/lang/Object;");
140         _allocateArrayAlign8 = k.getOrCreateStaticMethod("allocateArrayAlign8", "(IILjava/lang/Object;)Ljava/lang/Object;");
141     }
142 }